//
//  CompanyDetailViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/15.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "CompanyDetailViewController.h"
#import "CompanyTopTableViewCell.h"
#import "PersonGrowViewController.h"
#import "LocationTableViewCell.h"
#import "CompanyLcTableViewCell.h"
#import "CompanyPjTableViewCell.h"
#import "CompanyEvTableViewCell.h"
#import "CompanySJTableViewCell.h"
#import "HomeDetailViewController.h"
#import "GushiPinpaiViewController.h"
#import "JiaRuView.h"
#import "ChatViewController.h"
#import "PicTableViewCell.h"
#import "CompanyStoryTableViewCell.h"
#import <UMShare/UMShare.h>

@interface CompanyDetailViewController ()<UITableViewDelegate,UITableViewDataSource,CompanyButtonDelegate,CompanyEvTableViewCellDelegate,CompanySJTableViewCellDelegate,CompanyStoreShareDelegate,jiaruFinishDelegate>
{
    UITableView * contentTableView;
    NSDictionary * contentData;
    UIBarButtonItem * collectItem;
}
@end

@implementation CompanyDetailViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        
    }
    return self;
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    
    // Do any additional setup after loading the view.
    contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    contentTableView.delegate = self;
    contentTableView.dataSource = self;
    contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:contentTableView];
    [self request];
}

-(void)request
{
    [NetRequestCenter companycenterwithtaskid:self.contentId result:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            NSString * favsss = [NSString stringWithFormat:@"%@",[[resultDic objectForKey:@"data"]objectForKey:@"Favorite"]];
            
            
            
            if ([favsss isEqualToString:@"1"])
            {
                UIBarButtonItem * chatItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"chat"] style:UIBarButtonItemStylePlain target:self action:@selector(chatAction)];
                self->collectItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"collected"] style:UIBarButtonItemStylePlain target:self action:@selector(favAction)];
                self.navigationItem.rightBarButtonItems = @[chatItem,collectItem];
            }
            else
            {
                UIBarButtonItem * chatItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"chat"] style:UIBarButtonItemStylePlain target:self action:@selector(chatAction)];
                self->collectItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"collect"] style:UIBarButtonItemStylePlain target:self action:@selector(favAction)];
                self.navigationItem.rightBarButtonItems = @[chatItem,collectItem];
            }
            contentData = [resultDic objectForKey:@"data"];
            [contentTableView reloadData];
            self.navigationItem.title = [contentData objectForKey:@"NickName"];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

-(void)shareCompany
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * item1 = [UIAlertAction actionWithTitle:@"QQ" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self qqAction];
    }];
    UIAlertAction * item2 = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self wechatFriendAction];
    }];

    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:item1];
    [alertController addAction:item2];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)wechatFriendAction
{
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    NSString * description = [NSString stringWithFormat:@"%@——品牌故事",[contentData objectForKey:@"NickName"]];
    NSString * imageUrl = [NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[contentData objectForKey:@"HeadPic"]];
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"蓄锐广场" descr:description thumImage:imageUrl];
    //设置网页地址
    shareObject.webpageUrl = [NSString stringWithFormat:@"https://app.xrsquare.com/v180615/share/story?uid=%@",self.contentId];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

-(void)qqAction
{
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    NSString * description = [NSString stringWithFormat:@"%@——品牌故事",[contentData objectForKey:@"NickName"]];
    NSString * imageUrl = [NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[contentData objectForKey:@"HeadPic"]];
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"蓄锐广场" descr:description thumImage:imageUrl];    //设置网页地址
    shareObject.webpageUrl = [NSString stringWithFormat:@"https://app.xrsquare.com/v180615/share/story?uid=%@",self.contentId];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}


-(void)chatAction
{
    ChatViewController * chat = [[ChatViewController alloc]init];
    chat.targetUid = self.contentId;
    chat.contentid = [[contentData objectForKey:@"privately"]objectForKey:@"groupID"];
    chat.targetName = [[contentData objectForKey:@"privately"]objectForKey:@"RightName"];
    chat.targetPic = [[contentData objectForKey:@"privately"]objectForKey:@"RightHeadPic"];
    [self.navigationController pushViewController:chat animated:YES];
}

-(void)unFavAction
{
    [NetRequestCenter delfavoritecompanyuid:self.contentId result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:[resultDic objectForKey:@"message"]];
        } else {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

-(void)favAction
{
    [NetRequestCenter favoritecompanyuid:self.contentId result:^(BOOL success, NSDictionary *resultDic) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:[resultDic objectForKey:@"message"]];
        } else {
            [SVProgressHUD showErrorWithStatus:[resultDic objectForKey:@"message"]];
        }
    }];
}

-(void)lichengAction
{
    PersonGrowViewController * grow = [[PersonGrowViewController alloc]init];
    grow.contentId = self.contentId;
    grow.type = @"6";
    grow.companyName = [contentData objectForKey:@"NickName"];
    grow.contentTargetselector = @"company";
    grow.logoUrl = [NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[contentData objectForKey:@"HeadPic"]];
    [self.navigationController pushViewController:grow animated:YES];
}

-(void)jiaruAction
{
    JiaRuView * jiaru = [[JiaRuView alloc]initWithFrame:CGRectZero];
    jiaru.contentId = self.contentId;
    jiaru.delegate = self;
    [self.view addSubview:jiaru];
}

-(void)jiaruFinishAction
{
    [self request];
}


#pragma mark - tableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return [CompanyTopTableViewCell heightWithData:contentData];
    }
    else if (indexPath.row == 1)
    {
        return [PicTableViewCell heightWithData:[contentData objectForKey:@"Web_Company_History_Pic"]];
        
    }
    else if (indexPath.row == 2)
    {
        return [CompanyStoryTableViewCell heightWithData:[contentData objectForKey:@"Web_Compnay_History_Text"]];
    }
    else if (indexPath.row == 3)
    {
        return [CompanyLcTableViewCell height];
    }
    
    else if (indexPath.row == 4)
    {
        return [CompanyPjTableViewCell height];
    }
    else if (indexPath.row == 5)
    {
        return [CompanyEvTableViewCell heightWithData:[contentData objectForKey:@"ActivityArray"]];
    }
    else
    {
        return [CompanySJTableViewCell heightWithData:[contentData objectForKey:@"JobArray"]];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        static NSString * identifer = @"CompanyTopTableViewCell";
        CompanyTopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[CompanyTopTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.delegate = self;
        [cell loadData:contentData];
        return cell;
    }
    else if (indexPath.row == 1)
    {
        static NSString * identifer = @"PicTableViewCell";
        PicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[PicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        [cell loadData:[contentData objectForKey:@"Web_Company_History_Pic"]];
        return cell;
    }
    else if (indexPath.row == 2)
    {
        static NSString * identifer = @"CompanyStoryTableViewCell";
        CompanyStoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[CompanyStoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.delegate = self;
        [cell loadData:[contentData objectForKey:@"Web_Compnay_History_Text"]];
        return cell;
    }
    else if (indexPath.row == 3)
    {
        static NSString * identifer = @"CompanyLcTableViewCell";
        CompanyLcTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[CompanyLcTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        [cell loadData:[contentData objectForKey:@"Web_Company_Address"]];
        return cell;
    }
    else if (indexPath.row == 4)
    {
        static NSString * identifer = @"CompanyPjTableViewCell";
        CompanyPjTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[CompanyPjTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        [cell loadData:[NSString stringWithFormat:@"共%@条评价",[contentData objectForKey:@"EvaluateNumber"]]];
        return cell;
    }
    else if (indexPath.row == 5)
    {
        static NSString * identifer = @"CompanyEvTableViewCell";
        CompanyEvTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[CompanyEvTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.delegate = self;
        [cell loadData:[contentData objectForKey:@"ActivityArray"]];
        return cell;
    }
    else
    {
        static NSString * identifer = @"CompanySJTableViewCell";
        CompanySJTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifer];
        if (!cell) {
            cell = [[CompanySJTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        }
        cell.delegate = self;
        [cell loadData:[contentData objectForKey:@"JobArray"]];
        return cell;
    }
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(void)gushipinpai
{
    GushiPinpaiViewController * gushi = [[GushiPinpaiViewController alloc]init];
    gushi.contentId = self.contentId;
    [self.navigationController pushViewController:gushi animated:YES];
}

-(void)selectedContent:(NSDictionary *)contennt
{
    HomeDetailViewController * home = [[HomeDetailViewController alloc]init];
    home.contentid = [contennt objectForKey:@"TaskID"];
    [self.navigationController pushViewController:home animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
